//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CapFloorTermVolatilityStructure.h"
using namespace Cephei::QL::Termstructures::Volatility::Capfloor;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/VolatilityTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::CCapFloorTermVolatilityStructure (boost::shared_ptr<QuantLib::CapFloorTermVolatilityStructure>& childNative, Object^ owner) : CVolatilityTermStructure(CCapFloorTermVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phCapFloorTermVolatilityStructure = NULL;
#endif
	_ppCapFloorTermVolatilityStructure = &childNative;
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCapFloorTermVolatilityStructure));
}
Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::CCapFloorTermVolatilityStructure (QuantLib::CapFloorTermVolatilityStructure& childNative, Object^ owner) : CVolatilityTermStructure(CCapFloorTermVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phCapFloorTermVolatilityStructure = NULL;
#endif
	_ppCapFloorTermVolatilityStructure = new boost::shared_ptr<QuantLib::CapFloorTermVolatilityStructure> (&childNative);
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCapFloorTermVolatilityStructure));
    _CapFloorTermVolatilityStructureOwner = owner;
    _VolatilityTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::CCapFloorTermVolatilityStructure (CCapFloorTermVolatilityStructure^ copy) : CVolatilityTermStructure(CCapFloorTermVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phCapFloorTermVolatilityStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCapFloorTermVolatilityStructure = new boost::shared_ptr<QuantLib::CapFloorTermVolatilityStructure> (copy->GetShared());
        _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCapFloorTermVolatilityStructure));
    }
}
Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::CCapFloorTermVolatilityStructure (System::Type^ t) : CVolatilityTermStructure(CCapFloorTermVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phCapFloorTermVolatilityStructure = NULL;
#endif
	if (!t->IsSubclassOf(CCapFloorTermVolatilityStructure::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::CCapFloorTermVolatilityStructure (QuantLib::Handle<QuantLib::CapFloorTermVolatilityStructure>& childNative, Object^ owner)  : CVolatilityTermStructure(CCapFloorTermVolatilityStructure::typeid)
{
	_phCapFloorTermVolatilityStructure = &childNative;
	_ppCapFloorTermVolatilityStructure = &static_cast<boost::shared_ptr<QuantLib::CapFloorTermVolatilityStructure>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCapFloorTermVolatilityStructure));
    _CapFloorTermVolatilityStructureOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::CCapFloorTermVolatilityStructure (QuantLib::Handle<QuantLib::CapFloorTermVolatilityStructure> childNative)  : CVolatilityTermStructure(CCapFloorTermVolatilityStructure::typeid)
{
	_phCapFloorTermVolatilityStructure = &childNative;
	_ppCapFloorTermVolatilityStructure = &static_cast<boost::shared_ptr<QuantLib::CapFloorTermVolatilityStructure>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCapFloorTermVolatilityStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::CCapFloorTermVolatilityStructure (QuantLib::CapFloorTermVolatilityStructure childNative)  : CVolatilityTermStructure(CCapFloorTermVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phCapFloorTermVolatilityStructure = NULL;
#endif
	_ppCapFloorTermVolatilityStructure = new boost::shared_ptr<QuantLib::CapFloorTermVolatilityStructure> (new QuantLib::CapFloorTermVolatilityStructure (childNative));
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCapFloorTermVolatilityStructure));
}
#endif

Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::~CCapFloorTermVolatilityStructure ()
{
    if (_ppCapFloorTermVolatilityStructure != NULL)
    {
	    delete _ppCapFloorTermVolatilityStructure;
        _ppCapFloorTermVolatilityStructure = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::!CCapFloorTermVolatilityStructure ()
{
    if (_ppCapFloorTermVolatilityStructure != NULL)
    {
	    delete _ppCapFloorTermVolatilityStructure;
    }
}
QuantLib::CapFloorTermVolatilityStructure& Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::GetReference ()
{
    if (_ppCapFloorTermVolatilityStructure == NULL) throw gcnew NativeNullException ();
	return **_ppCapFloorTermVolatilityStructure;
}
boost::shared_ptr<QuantLib::CapFloorTermVolatilityStructure>& Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::GetShared ()
{
    if (_ppCapFloorTermVolatilityStructure == NULL) throw gcnew NativeNullException ();
	return *_ppCapFloorTermVolatilityStructure;
}
QuantLib::CapFloorTermVolatilityStructure* Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::GetPointer ()
{
    if (_ppCapFloorTermVolatilityStructure == NULL) throw gcnew NativeNullException ();
	return &**_ppCapFloorTermVolatilityStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CapFloorTermVolatilityStructure>& Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::GetHandle ()
{
	if (_phCapFloorTermVolatilityStructure == NULL)
	{
		_phCapFloorTermVolatilityStructure = new Handle<QuantLib::CapFloorTermVolatilityStructure> (*_ppCapFloorTermVolatilityStructure);
	}
	return *_phCapFloorTermVolatilityStructure;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::HasNative () 
{
	return (_ppCapFloorTermVolatilityStructure != NULL);
}

Double Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::Volatility (Double t, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCapFloorTermVolatilityStructure)->volatility ( _t,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::Volatility (DateTime end, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _end = (QuantLib::Date)ValueHelper::Convert (end);
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCapFloorTermVolatilityStructure)->volatility ( _end,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolatilityStructure::Volatility (Cephei::QL::Times::IPeriod^ length, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _Clength;
    try
    {
        _Clength = safe_cast<CPeriod^> (length);
        _Clength->Lock();
        QuantLib::Period& _length = static_cast<QuantLib::Period&> (_Clength->GetReference ()); 
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCapFloorTermVolatilityStructure)->volatility ( _length,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Clength != nullptr) _Clength->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

